export const getRegion = param => {
  let regiondata = param
  let restaurants1 = []
  let restaurants2 = []
  let restaurants3 = []
  for (let i = 0; i < regiondata[0].children.length; i++) {
    let regionlist1 = {}
    regionlist1.value = regiondata[0].children[i].name
    regionlist1.address = 1
    restaurants1[i] = regionlist1
    if (regiondata[0].children[i].children) {
      for (let j = 0; j < regiondata[0].children[i].children.length; j++) {
        let regionlist2 = {}
        regionlist2.value = regiondata[0].children[i].children[j].name
        regionlist2.address = 2
        restaurants2[i] = regionlist2
        if (regiondata[0].children[i].children[j].children) {
          for (let k = 0; k < regiondata[0].children[i].children[j].children.length; k++) {
            let regionlist3 = {}
            regionlist3.value = regiondata[0].children[i].children[j].children[k].name
            regionlist3.address = 3
            restaurants3[i] = regionlist3
          }
        }
      }
    }
  }
  let restaurants = []
  restaurants[0] = restaurants1
  restaurants[1] = restaurants2
  restaurants[2] = restaurants3
  return restaurants
}
export const getNextRegion = param => {
  let item = param[1]
  let data = param[0]
  let restaurants2 = []
  let restaurants3 = []
  for (let i = 0; i < data[0].children.length; i++) {
    if (item.address === 1 && item.value === data[0].children[i].name) {
      for (let j = 0; j < data[0].children[i].children.length; j++) {
        let regionlist2 = {}
        regionlist2.value = data[0].children[i].children[j].name
        regionlist2.address = 2
        restaurants2[j] = regionlist2
        if (j === 0 && data[0].children[i].children[0].children) {
          for (let k = 0; k < data[0].children[i].children[0].children.length; k++) {
            let regionlist3 = {}
            regionlist3.value = data[0].children[i].children[0].children[k].name
            regionlist3.address = 3
            restaurants3[k] = regionlist3
          }
        }
      }
    } else {
      for (let j = 0; j < data[0].children[i].children.length; j++) {
        if (data[0].children[i].children[j].name === item.value) {
          for (let k = 0; k < data[0].children[i].children[j].children.length; k++) {
            let regionlist4 = {}
            regionlist4.value = data[0].children[i].children[j].children[k].name
            regionlist4.address = 3
            restaurants3[k] = regionlist4
          }
        }
      }
    }
  }
  let arr = []
  arr[0] = restaurants2
  arr[1] = restaurants3
  return arr
}
export const getOptionDataType = rdata => {
  let regionData = []
  for (let i = 0; i < rdata.length; i++) {
    let obj = {}
    obj.value = rdata[i]['id']
    if (!rdata[i]['name']) obj.label = rdata[i]['agencyName']
    else obj.label = rdata[i]['name']
    if (rdata[i].children) {
      obj.children = []
      for (let j = 0; j < rdata[i].children.length; j++) {
        var cobj = {}
        cobj.value = rdata[i].children[j]['id']
        cobj.label = rdata[i].children[j]['name']
        obj.children[j] = cobj
        if (rdata[i].children[j].children) {
          obj.children[j].children = []
          for (let k = 0; k < rdata[i].children[j].children.length; k++) {
            var ccobj = {}
            ccobj.value = rdata[i].children[j].children[k]['id']
            ccobj.label = rdata[i].children[j].children[k]['name']
            obj.children[j].children[k] = ccobj
          }
        }
      }
    }
    regionData[i] = obj
  }
  return regionData
}
// 递归
export const recursion = data => {
  let id = data.id
  let regionData = data.items
  let arr = []
  for (let i = 0; i < regionData.length; i++) {
    if (id === regionData[i].id) {
      let allId = {}
      if (regionData[i].allId) allId.allId = regionData[i].allId
      else allId.allId = regionData[i].parentAllId
      allId.levelCode = regionData[i].levelCode
      allId.gridCode = regionData[i].gridCode
      allId.nextGridData = regionData[i].children ? regionData[i].children : null
      arr.push(regionData[i].id)
      arr.push(allId)
      break
    } else if (regionData[i].children) {
      let rdata = {}
      rdata.id = id
      rdata.items = regionData[i].children
      arr.push(regionData[i].id)
      let arr1 = recursion(rdata)
      if (arr1.length !== 0) {
        for (let j = 0; j < arr1.length; j++) {
          arr.push(arr1[j])
        }
        break
      } else {
        arr = []
      }
    }
  }
  return arr
}

const strConduct = param => {
  let beforeOrder = param.beforeOrder
  let afterOrder = param.afterOrder
  let gridData = param.gridData
  let gridCode = param.gridCode
  let lastCode = param.lastCode
  let resGridCode = ''
  let pCode = gridCode.slice(0, beforeOrder)
  let baseArr = []
  for (let j = 0; j < param.gridData.length; j++) {
    if (!gridData[j].gridCode || gridData[j].gridCode.length !== 8) continue
    let baseGridCode = gridData[j].gridCode.slice(beforeOrder, afterOrder)
    baseArr.push(baseGridCode)
  }
  if (baseArr.length === 0) {
    resGridCode = pCode + '01' + lastCode
    return resGridCode
  }
  let max = baseArr[0]
  for (let key in baseArr) {
    if (baseArr.hasOwnProperty(key)) {
      max = parseInt(max) > parseInt(baseArr[key]) ? max : baseArr[key]
    }
  }
  let baseGridArr = max.split('')
  let len = baseGridArr.length
  let end = baseGridArr[len - 1]
  let booby = baseGridArr[len - 2]
  let price = parseInt(end) + 1
  end = price === 10 ? '0' : price.toString()
  booby = price === 10 ? parseInt(booby) + 1 : booby
  resGridCode = pCode + booby.toString() + end + lastCode
  return resGridCode
}

export const getAllRegion = data => {
  if (data && data.id && data.items) {
    let arr = recursion(data)
    if (data.isGetAllId) {
      let index = arr.length - 1
      let allId = arr[index].allId
      allId = allId.replace(/[[]/g, '')
      allId = allId.replace(/]/g, '')
      return allId
    } else if (data.isGetGridCode) {
      let resGridCode = ''
      if (arr.length === 0) return false
      let n = arr.length
      let gridCode = arr[n - 1].gridCode
      let levelCode = arr[n - 1].levelCode
      if (!gridCode) {
        return resGridCode
      }
      let nextGridData = arr[n - 1].nextGridData
      if (nextGridData) {
        let param = {}
        param.gridData = nextGridData
        param.gridCode = gridCode
        switch (levelCode) {
          case '06':
            param.beforeOrder = 6
            param.afterOrder = 8
            param.lastCode = ''
            break
          case '05':
            param.beforeOrder = 4
            param.afterOrder = 6
            param.lastCode = '00'
            break
          default:
            param.beforeOrder = 2
            param.afterOrder = 4
            param.lastCode = '0000'
            break
        }
        return strConduct(param)
      } else {
        let param = {}
        param.gridCode = gridCode
        param.gridData = []
        return strConduct(param)
      }
    } else if (data.isGetLevel) {
      if (arr.length === 0) return false
      let n = arr.length
      let levelCode = arr[n - 1].levelCode
      return levelCode
    } else {
      let arr1 = []
      for (var i = 0; i < arr.length - 1; i++) {
        arr1.push(arr[i])
      }
      return arr1
    }
  } else {
    return []
  }
}

export const recursionObj = data => {
  let id = data.id
  let regionData = data.items
  let arr = []
  for (let i = 0; i < regionData.length; i++) {
    if (id === regionData[i].id) {
      let allId = {}
      if (regionData[i].allId) allId.allId = regionData[i].allId
      else allId.allId = regionData[i].parentAllId
      allId.levelCode = regionData[i].levelCode
      allId.gridCode = regionData[i].gridCode
      allId.name = regionData[i].name
      allId.id = regionData[i].id
      allId.nextGridData = regionData[i].children ? regionData[i].children : null
      // arr.push(regionData[i].id)
      arr.push(allId)
      break
    } else if (regionData[i].children) {
      let rdata = {}
      let temp = {}
      rdata.id = id
      rdata.items = regionData[i].children
      temp.levelCode = regionData[i].levelCode
      temp.gridCode = regionData[i].gridCode
      temp.name = regionData[i].name
      temp.id = regionData[i].id
      temp.children = regionData[i].children
      arr.push(temp)
      let arr1 = recursionObj(rdata)
      if (arr1.length !== 0) {
        for (let j = 0; j < arr1.length; j++) {
          arr.push(arr1[j])
        }
        break
      } else {
        arr = []
      }
    }
  }
  return arr
}
export const getAllRegionObj = data => {
  let arr = recursionObj(data)
  if (data.isGetAllId) {
    let index = arr.length - 1
    let allId = arr[index].allId
    allId = allId.replace(/[[]/g, '')
    allId = allId.replace(/]/g, '')
    return allId
  } else if (data.isGetGridName) {
    let name = []
    for (let i = 0; i < arr.length; i++) {
      name.push(arr[i].name)
    }
    return name
  } else if (data.isGetGridItem) {
    return arr
  } else if (data.isGetGridCode) {
    let resGridCode = ''
    if (arr.length === 0) return false
    let n = arr.length
    let gridCode = arr[n - 1].gridCode
    let levelCode = arr[n - 1].levelCode
    if (!gridCode) {
      return resGridCode
    }
    let nextGridData = arr[n - 1].nextGridData
    if (nextGridData) {
      let param = {}
      param.gridData = nextGridData
      param.gridCode = gridCode
      switch (levelCode) {
        case '06':
          param.beforeOrder = 6
          param.afterOrder = 8
          param.lastCode = ''
          break
        case '05':
          param.beforeOrder = 4
          param.afterOrder = 6
          param.lastCode = '00'
          break
        default:
          param.beforeOrder = 2
          param.afterOrder = 4
          param.lastCode = '0000'
          break
      }
      return strConduct(param)
    }
  } else if (data.isGetLevel) {
    if (arr.length === 0) return false
    let n = arr.length
    let levelCode = arr[n - 1].levelCode
    return levelCode
  } else {
    let arr1 = []
    for (var i = 0; i < arr.length; i++) {
      let tempObj = {}
      tempObj.name = arr[i].name
      tempObj.gridCode = arr[i].gridCode
      arr1.push(tempObj)
    }
    return arr1
  }
}
